package com.dxl.lanju.dao;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.dxl.common.model.Page;
import com.dxl.common.utils.SqlBuilder;
import com.dxl.common.utils.Utils;
import com.dxl.dao.common.BaseDao;
import com.lanju.entity.User;

/**
 * 用户管理DAO
 * @author 邓小林
 */
@Repository
public class UserDao extends BaseDao {
	private static final Logger LOG = Logger.getLogger(UserDao.class);
	
	public Page<User> list(String keyword, Integer state, Integer pageNo, Integer pageSize) {
		SqlBuilder sb = new SqlBuilder("select * from user where 1 = 1");
		if (Utils.isNotBlank(keyword)) {
			keyword = keyword.trim();
			sb.appendStatement(" and (");
			sb.appendStatement(" name like ").appendParamValue("%" + keyword + "%");
			sb.appendStatement(" or mobile like ").appendParamValue("%" + keyword + "%");
			sb.appendStatement(" or login_name like ").appendParamValue("%" + keyword + "%");
			sb.appendStatement(" or car_num like ").appendParamValue("%" + keyword + "%");
			sb.appendStatement(")");
		}
		if (Utils.isNotNull(state)) {
			sb.appendStatement(" and state = ").appendParamValue(state);
		}
		sb.appendStatement(" order by created_time desc");
		return this.list(User.class, sb.toStatement(), sb.toParams(), pageNo, pageSize);
	}
	
	public boolean exist(String account) {
		SqlBuilder sb = new SqlBuilder("select * from user where ")
		.appendStatement(" mobile = ").appendParamValue(account)
		.appendStatement(" or login_name = ").appendParamValue(account)
		.appendStatement(" or car_num = ").appendParamValue(account);
		return this.count(sb.toStatement(), sb.toParams()) > 0;
	}
}
